package cn.dengta.webapp.user.entity.task;

import java.util.Date;

import cn.dengta.webapp.base.entity.AbstractBizLog;
import cn.dengta.webapp.base.entity.BaseEntity;
import cn.dengta.webapp.base.entity.Operation;
import cn.dengta.webapp.user.entity.task.TaskRule.TaskRuleType;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.NumberUtil;

/**
 * 任务账户记录表
 *
 * @author lgy
 * @since 2022-03-05
 */
@Setter
@Getter
@Accessors(chain = true)
public class TaskAccountLog extends AbstractBizLog {

	/** 业务类型 */
	private TaskRuleType ruleType;
	/** 操作目标类型 */
	private String assocType;
	/** 操作目标ID */
	private Long assocId;
	/** 总钻石 */
	private Integer totalCoin;
	/** 可用钻石 */
	private Integer activeCoin;
	/** 冻结钻石 */
	private Integer frozenCoin;
	/** 改变钻石 */
	private Integer changeCoin;
	/** 完成数 */
	private Integer completeCount;
	/** 流向：1=收入；0=不变；-1=支出 */
	private Integer flow;

	public TaskAccountLog init(TaskAccount account, Integer changeCoin, char total, char active, char frozen) {
		this.changeCoin = (active == '-' && changeCoin <= 0) || changeCoin >= 0 ? changeCoin : -changeCoin;
		this.totalCoin = account.opTotalAccount(total, changeCoin);
		this.activeCoin = account.opActiveAccount(active, changeCoin);
		this.frozenCoin = account.opFrozenAccount(frozen, changeCoin);
		return this;
	}

	public TaskAccountLog init(Long userId, Operation type, BaseEntity assocBean, String remark, Date now) {
		this.assocType = assocBean.entityName();
		this.assocId = assocBean.getId();
		this.completeCount = NumberUtil.getInt(completeCount, 0) + 1;
		super.init(userId, type, null, remark, now);
		return this;
	}

	public TaskAccountLog initStatus() {
		super.initActiveStatus();
		return this;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public static final String USER_ID = "user_id";
	public static final String RULE_TYPE = "rule_type";
	public static final String TYPE = "type";
	public static final String ASSOC_TYPE = "assoc_type";
	public static final String ASSOC_ID = "assoc_id";
	public static final String TOTAL_COIN = "total_coin";
	public static final String ACTIVE_COIN = "active_coin";
	public static final String FROZEN_COIN = "frozen_coin";
	public static final String CHANGE_COIN = "change_coin";
	public static final String COMPLETE_COUNT = "complete_count";
	public static final String FLOW = "flow";
	public static final String DETAIL = "detail";
	public static final String REMARK = "remark";

}
